<template>
  <BasicModal v-bind="$attrs" destroyOnClose title="换绑手机" width="40%" :okButtonProps="{ disabled: false }" @register="registerModal" :destroyOnClose="true" @ok="handleSubmit">
    <BasicForm @register="registerForm"></BasicForm>
  </BasicModal>
</template>
<script lang="ts" setup>
import { BasicModal, useModalInner } from '@apps/antd-admin/src/components/modal';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/form';
import { changeMobileFormSchema } from '../data/data';
import { ref, unref } from 'vue';
import { changeMobile, getUserInfo } from '../data/api';

const emit = defineEmits(['success', 'register']);
const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 90,
  schemas: changeMobileFormSchema,
  showActionButtonGroup: false,
});
const isUpdate = ref(true);
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  resetFields();
  data.value = !!data?.isUpdate;

  if (unref(isUpdate)) {
    let record = data.record;
    let queryData = await getUserInfo({ userId: record.userId });
    setFieldsValue({
      ...queryData,
    });
  }
});

async function handleSubmit() {
  try {
    const values = await validate();
    console.info(values);
    setModalProps({ confirmLoading: true });
    if (unref(isUpdate)) {
      await changeMobile(values);
    }
    closeModal();
    emit('success', false);
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
